<template>
  <cml-module-item :title="titleText" :titleMore="titleMore" v-bind="$attrs" v-on="$listeners">
    <a-switch
      size="small"
      :disabled="false"
      :loading="rloading"
      :checked="isStart"
      @change="change($event,true)"
    />
    <template v-slot:detail>
      <voice-recognize-more></voice-recognize-more>
    </template>
  </cml-module-item>
</template>

<script>
import { mapGetters, mapMutations } from 'vuex'
import CmlModuleItem from '../../cml-module-item'
import VoiceRecognizeMore from './morePanel'
import { Switch, message } from 'ant-design-vue'
import EventBus from '@/assets/js/app/cmlpanel/util/bus.js'
import * as anov from 'anov-core'

export default {
  name: 'VoiceRecognize',
  inheritAttrs: false,
  components: {
    ASwitch: Switch,
    CmlModuleItem,
    VoiceRecognizeMore
  },
  model: {},
  props: {},
  data() {
    return {
      rmessage: '', //错误消息
      rloading: false, //启动加载状态
      activeComp: '',
      hasOpened: false
    }
  },
  computed: {
    ...anov.voiceRecognizeServer.mapState(['isStart', 'iatType']),
    ...anov.envServer.mapState(['audioStatus']),
    titleText() {
      return this.$t('cmlpanel.recognition')
    },
    titleMore() {
      return this.$t('cmlpanel.recognition')
    }
  },
  watch: {
    isStart(v, ov) {
      if (v !== ov) {
        this.change(v)
      }
    }
  },
  created() {},
  mounted() {
    //初始化状态
    this.isStart && this.change(this.isStart)
    const eventName = 'voiceReconginzeStateChange'
    anov.envServer.addEventListener('offLine', () => {
      if (this.iatType == 'xf' && this.isStart) {
        app.msg({
          type: 'error',
          content: '网络错误，请确保连接到远程服务器。'
        })
        anov.voiceRecognizeServer.stop()
        EventBus.$emit(eventName, 'abort')
        this.$result.close()
        this.hasOpened = false
      }
    })
    anov.voiceRecognizeServer.addEventListener('onStart', () => {
      if (!this.hasOpened) {
        this.$result({ anchor: '#app' })
        this.hasOpened = true
      }
      EventBus.$emit(eventName, 'start')
    })
    anov.voiceRecognizeServer.addEventListener('onSoundStart', () => {
      setTimeout(() => {
        this.rloading = false
      }, 10)
      EventBus.$emit(eventName, 'soundstart')
    })
    anov.voiceRecognizeServer.addEventListener('onResult', e => {
      EventBus.$emit(eventName, 'result', e.result)
    })
    anov.voiceRecognizeServer.addEventListener('onResultMatch', e => {
      let cmd = e.result
      //扩展兼容 如果有注册exec 先执行,
      cmd.exec && cmd.exec(anov.actionServer.getCurrentContext(), cmd.params)
      //如果有动作 则执行动作
      cmd.action && anov.actionServer.exec(cmd.action, cmd.params)
      EventBus.$emit(eventName, 'result', cmd)
    })
    anov.voiceRecognizeServer.addEventListener('onStop', () => {
      EventBus.$emit(eventName, 'abort')
      this.$result.close()
      this.hasOpened = false
    })
    anov.voiceRecognizeServer.addEventListener('onError', e => {
      this.onError(e)
      EventBus.$emit(eventName, 'abort')
      this.$result.close()
      this.hasOpened = false
    })
  },
  methods: {
    change(isStart, byUser) {
      if (isStart) {
        //启动
        if (!this.audioStatus) {
          app.msg({type: 'error', content: '未检测到麦克风，无法开启语音识别'})
          return
        }
        this.rloading = isStart
        anov.voiceRecognizeServer.start()
        EventBus.$emit('voiceReconginzeStateChange', 'starting')
      } else {
        //关闭
        anov.voiceRecognizeServer.stop()
        this.$result.close()
        this.hasOpened = false
      }
      if (!byUser) {
        this.podcast(this.isStart)
      }
    },
    onError(e) {
      setTimeout(() => {
        this.rloading = false
      }, 10)
      app.msg({ type: 'error', content: e.result })
    },
    podcast(isStart) {
      let msg =
        this.titleText +
        (isStart ? this.$t('cmlpanel.start') : this.$t('cmlpanel.stop'))
      anov.voiceFeedbackServer.speak(msg)
      app.msg({ type: 'info', content: msg })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>